package com.mantru.iotpot.setting;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.Cook.ManualCookActivity;
import com.mantru.iotpot.Cook.adapter.NunberKeyAdapter;
import com.mantru.iotpot.MakeSeekBar;
import com.mantru.iotpot.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class WashingSettingActivity extends AppCompatActivity implements View.OnClickListener , NunberKeyAdapter.OnNumberClickListener{

    private LinearLayout back;
    private RelativeLayout put_water;
    private TextView water_text;
    private RelativeLayout set_time;
    private TextView time_text;
    private MakeSeekBar seekBar1;
    private MakeSeekBar seekBar2;
    private Button wash_way1;
    private Button wash_way2;
    private Button wash_way3;
    private Button save;
    private LinearLayout numberkey_box;
    private TextView number_text;
    private TextView unit_text;
    private GridView numberkey;
    private Button confirm;
    private int setting = 1;//setting为1表示输入重量，2为输入时间
    private ApplicationClass ac;
    private int parsedTime;
    private int parsedWater;
    private int fire_value;
    private int speed_value;
    private int rotation_value;
    private ColorStateList normalColor;
    private ColorStateList minColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_washing_setting);
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        normalColor = ContextCompat.getColorStateList(WashingSettingActivity.this, R.color.normal_seekbar_color); // 正常颜色
        minColor = ContextCompat.getColorStateList(WashingSettingActivity.this, R.color.min_seekbar_color);// 最小值颜色
        initialization();//初始化控件
        getJson();//获取json数据
        star_ui();//给控件赋初始值
    }

    //初始化控件
    private void initialization(){
        back = findViewById(R.id.washing_setting_back);
        put_water = findViewById(R.id.washing_setting_put_water);
        water_text = findViewById(R.id.washing_setting_put_water_text);
        set_time = findViewById(R.id.washing_setting_time);
        time_text = findViewById(R.id.washing_setting_time_text);
        seekBar1 = findViewById(R.id.washing_setting_seekBar1);
        seekBar2 = findViewById(R.id.washing_setting_seekbar2);
        wash_way1 = findViewById(R.id.washing_setting_way_rotation1);
        wash_way2 = findViewById(R.id.washing_setting_way_rotation2);
        wash_way3 = findViewById(R.id.washing_setting_way_rotation3);
        save = findViewById(R.id.washing_setting_save);
        //自定义键盘控件初始化
        numberkey_box = findViewById(R.id.washing_setting_number_key_box);
        number_text = findViewById(R.id.washing_setting_number_text);
        unit_text = findViewById(R.id.washing_setting_text);
        numberkey = findViewById(R.id.washing_setting_gridview);
        confirm = findViewById(R.id.washing_setting_confirm);

        back.setOnClickListener(this);
        wash_way1.setOnClickListener(this);
        wash_way2.setOnClickListener(this);
        wash_way3.setOnClickListener(this);
        save.setOnClickListener(this);
        confirm.setOnClickListener(this);
        put_water.setOnClickListener(this);
        set_time.setOnClickListener(this);

        List<String> items = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            items.add(String.valueOf(i));
        }
        items.add(""); // 小数点
//        items.add("0");
//        items.add("删除"); // 删除键
        NunberKeyAdapter adapter = new NunberKeyAdapter(this,items,this);
        numberkey.setAdapter(adapter);

        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress < 1){
                    progress = 1;
                    seekBar1.setProgress(1);
                }
                // 根据进度切换颜色
                if (progress == 1) {
                    seekBar.setProgressTintList(minColor); // 设置为最小值颜色
                } else {
                    seekBar.setProgressTintList(normalColor); // 设置为正常颜色
                }
                fire_value = progress-1;
                Log.d("WashingSettingActivity", "fire_value: " + fire_value);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress < 1){
                    progress = 1;
                    seekBar2.setProgress(1);
                }
                // 根据进度切换颜色
                if (progress == 1) {
                    seekBar.setProgressTintList(minColor); // 设置为最小值颜色
                } else {
                    seekBar.setProgressTintList(normalColor); // 设置为正常颜色
                }
                speed_value = progress-1;
                Log.d("WashingSettingActivity", "progress2: " + speed_value);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.washing_setting_back){//点击返回按钮
            finish();
        } else if (v.getId() == R.id.washing_setting_put_water) {//点击设置用水量
            setting = 1;
            numberkey_box.setVisibility(View.GONE);
            unit_text.setText("克");
            save.setVisibility(View.GONE);
            numberkey_box.setVisibility(View.VISIBLE);
            number_text.setText(water_text.getText().toString());
        } else if (v.getId() == R.id.washing_setting_time) {//点击设置时间
            setting = 2;
            numberkey_box.setVisibility(View.GONE);
            unit_text.setText("秒");
            save.setVisibility(View.GONE);
            numberkey_box.setVisibility(View.VISIBLE);
            number_text.setText(time_text.getText().toString());
        } else if (v.getId() == R.id.washing_setting_confirm) {//点击数字键盘的确定按钮
            save.setVisibility(View.VISIBLE);
            numberkey_box.setVisibility(View.GONE);
            if (setting == 1){
                String currentValue = number_text.getText().toString();
                water_text.setText(currentValue);
            } else if (setting == 2) {
                String currentValue = number_text.getText().toString();
                time_text.setText(currentValue);
            }
        } else if (v.getId() == R.id.washing_setting_way_rotation1) {
            rotation_value = 0;
            setBackGround(rotation_value);
        }else if (v.getId() == R.id.washing_setting_way_rotation2) {
            rotation_value = 1;
            setBackGround(rotation_value);
        }else if (v.getId() == R.id.washing_setting_way_rotation3) {
            rotation_value = 2;
            setBackGround(rotation_value);
        } else if (v.getId() == R.id.washing_setting_save) {//点击保存
            parsedTime = Integer.parseInt(time_text.getText().toString());
            parsedWater = Integer.parseInt(water_text.getText().toString());
            ac.CleanData_Customer_SaveData(parsedTime, parsedWater, fire_value, speed_value, rotation_value);
            Log.d("WashingSettingActivity","parsedTime: " + parsedTime + " parsedWater: " + parsedWater +
                    " fire_value: " + fire_value + " speed_value: " + speed_value + " rotation_value: " + rotation_value);
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    //一个用于设置数字键盘点击事件的方法
    @Override
    public void onNumberClick(String num) {
        String currentValue = number_text.getText().toString();
        Log.d("number", "currentValue: " + currentValue);
        if (num.equals("删除")) {
//            if (currentValue.length() > 0) {
//                String newValue = currentValue.substring(0, currentValue.length() - 1);
//                if (newValue.isEmpty()) {
//                    number_text.setText("0"); // 如果删除后为空，设置为 "0"
//                } else {
//                    number_text.setText(newValue);
//                }
//            }
            number_text.setText("0");
        } else {
            // 如果当前值是 "0"，并且输入的是非零数字，则替换 "0"
            if (currentValue.equals("0")) {
                if (!num.equals("0")) {
                    number_text.setText(num);
                }
            } else {
                number_text.setText(currentValue + num);
            }
        }
    }

    //初始化各控件的值
    private void star_ui(){
        time_text.setText(String.valueOf(parsedTime));
        water_text.setText(String.valueOf(parsedWater));
        seekBar1.setProgress(fire_value + 1);
        if (fire_value + 1 == 1){
            seekBar1.setProgressTintList(minColor);
        }else {
            seekBar1.setProgressTintList(normalColor);
        }
        seekBar2.setProgress(speed_value + 1);
        if (speed_value + 1 == 1){
            seekBar2.setProgressTintList(minColor);
        }else {
            seekBar2.setProgressTintList(normalColor);
        }
        setBackGround(rotation_value);
    }


    private void getJson() {
        String json_data = ac.CleanData_Customer_GetData(); // 假设这个方法返回 JSON 数据字符串
        Log.d("WashingSettingActivity", "json_data: " + json_data);
        try {
            JSONObject jsonObject = new JSONObject(json_data);
            parsedTime = jsonObject.getInt("time");
            parsedWater = jsonObject.getInt("water");
            fire_value = jsonObject.getInt("fire_value");
            speed_value = jsonObject.getInt("gripper_speed_value");
            rotation_value = jsonObject.getInt("gripper_rotation_value");
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("WashingSettingActivity", "Error parsing JSON", e);
        }
    }

    private void setBackGround(int way){
        wash_way1.setBackgroundResource(R.drawable.button14);
        wash_way2.setBackgroundResource(R.drawable.button14);
        wash_way3.setBackgroundResource(R.drawable.button14);
        if (way == 0){
            wash_way1.setBackgroundResource(R.drawable.button13);
        } else if (way == 1) {
            wash_way2.setBackgroundResource(R.drawable.button13);
        } else if (way == 2) {
            wash_way3.setBackgroundResource(R.drawable.button13);
        }
    }
}